﻿var PageValidateUtil = window.PageValidateUtil || {};
PageValidateUtil = {
    errorSpanList: new Array(),
    errorMsgFix: "_error_msg_span",
    errorCount: 0,
    //input text类型验证 或者 textarea类型验证：正则表达式
    //validateMode:验证模式,0:不验证;1:及时验证;2:输入焦点验证;3:输入焦点验证和及时验证; (默认为1)
    TextRegexValidate: function(id, regex, errorMsg, errorMsgClassName, matchRegex, validateMode) {
        if (matchRegex == null)  //是需要匹配正则表达式还是不匹配正则表达式
            matchRegex = true;
        if (validateMode == null)
            validateMode = 1;
        var targetEle = document.getElementById(id);
        var parentEle = targetEle.parentNode;
        if (targetEle != null && (targetEle.tagName.toLowerCase() == "input" || targetEle.tagName.toLowerCase() == "textarea")) {
            //设置失去焦点时事件方法
            if ((validateMode & 2) > 0) {
                targetEle.onblur = function() {
                    var errorSpanId = id + PageValidateUtil.errorMsgFix;
                    var errorSpanEle = document.getElementById(errorSpanId);
                    var regexResult = regex.test(targetEle.value);
                    if (matchRegex != regexResult) {  //不满足正则表达式
                        PageValidateUtil.AddErrorMsg(errorMsg, errorMsgClassName, targetEle);
                        return false;
                    }
                    else {                           //验证通过
                        PageValidateUtil.RemoveErrorMsg(targetEle);
                        return true;
                    }
                }
            }
            //及时验证
            if ((validateMode & 1) > 0) {
                var errorSpanId = id + PageValidateUtil.errorMsgFix;
                var errorSpanEle = document.getElementById(errorSpanId);
                var regexResult = regex.test(targetEle.value);
                if (matchRegex != regexResult) {  //不满足正则表达式
                    PageValidateUtil.AddErrorMsg(errorMsg, errorMsgClassName, targetEle);
                    return false;
                }
                else {                           //验证通过
                    PageValidateUtil.RemoveErrorMsg(targetEle);
                    return true;
                }
            }
        }
        return null;
    },
    //自定义验证，
    //customerValidateFunc:自定义验证方法
    CustomerValidate: function(id, customerValidateFunc, errorMsg, errorMsgClassName, validateMode) {
        var targetEle = document.getElementById(id);
        if (validateMode == null)
            validateMode = 1;
        if ((validateMode & 1) > 0) {
            if (customerValidateFunc) {
                if (customerValidateFunc(targetEle) == true) {
                    PageValidateUtil.RemoveErrorMsg(targetEle);
                    return true;
                }
                else {
                    PageValidateUtil.AddErrorMsg(errorMsg, errorMsgClassName, targetEle);
                    return false;
                }
            }
        }
        if ((validateMode & 2) > 0) {
            if (customerValidateFunc && (targetEle.tagName.toLowerCase() == "input" || targetEle.tagName.toLowerCase() == "textarea")) {
                targetEle.onblur = function() {
                    if (customerValidateFunc(targetEle) == true) {
                        PageValidateUtil.RemoveErrorMsg(targetEle);
                        return true;
                    }
                    else {
                        PageValidateUtil.AddErrorMsg(errorMsg, errorMsgClassName, targetEle);
                        return false;
                    }
                }
            }
        }
        return null;
    },
    //select 验证 
    //invalidValue:select不允许选择的值
    SelectValidate: function(id, invalidValue, errorMsg, errorMsgClassName) {
        var selectEle = document.getElementById(id);
        if (selectEle.tagName.toLowerCase() == "select") {
            if (selectEle[selectEle.selectedIndex].value == invalidValue) {
                PageValidateUtil.AddErrorMsg(errorMsg, errorMsgClassName, selectEle);
                return false;
            }
            else {
                PageValidateUtil.RemoveErrorMsg(selectEle);
                return true;
            }
        }
        return null;
    },
    //组验证，组中必须要有一个通过验证
    //validateArray为需要验证的元素的数组，元素的格式{id:'',errorMsg:'',className:'',type:'',refFunc:'',otherArg:''}
    //其中:type:customer,select,regex,
    //对应:refFunc:customerValidateFunc,invalidValue,regex
    GroupValidate: function(validateArray) {
        var groupSuccessCount = 0;
        for (var i = 0; i < validateArray.length; i++) {
            var validateFunc = null;
            switch (validateArray[i].type) {
                case "customer":
                    {
                        validateFunc = PageValidateUtil.CustomerValidate;
                        break;
                    }
                case "select":
                    {
                        validateFunc = PageValidateUtil.SelectValidate;
                        break;
                    }
                case "regex":
                    {
                        validateFunc = PageValidateUtil.TextRegexValidate;
                        break;
                    }
                default:
                    {
                        break;
                    }
            }

            if (validateFunc != null) {
                var validateResult = validateFunc(validateArray[i].id, validateArray[i].refFunc, validateArray[i].errorMsg, validateArray[i].className, validateArray[i].otherArg, 1);
                if (validateResult == true) {
                    groupSuccessCount++;
                }
                else {
                    document.getElementById(validateArray[i].id + PageValidateUtil.errorMsgFix).style.display = "none";
                }
            }
        }
        if (groupSuccessCount <= 0) {
            for (var i = 0; i < validateArray.length; i++) {
                var errorMsgSpanEle = document.getElementById(validateArray[i].id + PageValidateUtil.errorMsgFix);
                errorMsgSpanEle.style.display = "";
                errorMsgSpanEle.style.color = "blue";
            }
        }

        return groupSuccessCount > 0;
    },
    AddErrorMsg: function(errorMsg, errorMsgClassName, sourceEle) {
        var parentEle = sourceEle.parentNode;
        var errorMsgId = sourceEle.id + PageValidateUtil.errorMsgFix;
        var errorSpanEle = document.getElementById(errorMsgId);
        if (errorSpanEle == null) {
            var newSpanEle = document.createElement("span");
            newSpanEle.className = (errorMsgClassName != null) ? errorMsgClassName : "";
            newSpanEle.innerHTML = errorMsg;
            newSpanEle.id = errorMsgId;
            if (parentEle.lastChild == sourceEle)
                parentEle.appendChild(newSpanEle);
            else
                parentEle.insertBefore(newSpanEle, sourceEle.nextSibling);
            PageValidateUtil.errorCount++;
            PageValidateUtil.errorSpanList.push(newSpanEle);
        }
    },
    RemoveErrorMsg: function(sourceEle) {
        var parentEle = sourceEle.parentNode;
        var errorMsgId = sourceEle.id + PageValidateUtil.errorMsgFix;
        var errorSpanEle = document.getElementById(errorMsgId);
        if (errorSpanEle != null) {
            parentEle.removeChild(errorSpanEle);
            PageValidateUtil.errorCount--;
            for (var i = 0; i < PageValidateUtil.errorSpanList.length; i++) {
                if (PageValidateUtil.errorSpanList[i] == errorSpanEle) {
                    PageValidateUtil.errorSpanList.splice(i, 1);
                    break;
                }
            }
        }
    },
    //初始话表单提交验证，只有当表单提交的时候才验证
    initSubmitValidates: function(validateFuncs, args) {
        if (theForm) {
            theForm.onsubmit = function(o) {
                if (validateFuncs.length == args.length) {
                    PageValidateUtil.errorCount = 0;
                    for (var i = 0; i < validateFuncs.length; i++) {
                        if (PageValidateUtil.hasOwnProperty(validateFuncs[i]) && (args[i] instanceof Array)) {
                            PageValidateUtil[validateFuncs[i]](args[i][0], args[i][1], args[i][2], args[i][3], args[i][4], 1);
                        }
                    }
                    if (PageValidateUtil.errorCount > 0) {
                        o = o || window.event;
                        PageValidateUtil.clearEventBubble(o);
                        return false;
                    }
                }
            }
        }
    },
    //初始化及时验证，方法调用的时候就验证
    initImidValidates: function(validateFuncs, args) {
        var errorcount = 0;
        if (validateFuncs.length == args.length) {
            for (var i = 0; i < validateFuncs.length; i++) {
                if (PageValidateUtil.hasOwnProperty(validateFuncs[i]) && (args[i] instanceof Array)) {
                    var result = PageValidateUtil[validateFuncs[i]](args[i][0], args[i][1], args[i][2], args[i][3], args[i][4], 1);
                    if (result == false)
                        errorcount++;
                }
            }
        }
        return errorcount;
    },
    //初始化获取焦点验证，暂时只有当 TextRegexValidate 类型的验证此方法才有效
    initFocusValidates: function(validateFuncs, args) {
        if (validateFuncs.length == args.length) {
            for (var i = 0; i < validateFuncs.length; i++) {
                if ( (args[i] instanceof Array)) {
                    PageValidateUtil[validateFuncs[i]](args[i][0], args[i][1], args[i][2], args[i][3], args[i][4], 2);
                }
            }
        }
    },
    //元素事件验证
    initElementsValidates: function(ids, eventName, validateFuncs, args) {
        if (ids.length > 0 && validateFuncs.length == args.length) {
            for (var i = 0; i < ids.length; i++) {
                var targetEle = document.getElementById(ids[i]);
                if ((typeof (targetEle[eventName])).toString() != "undefined" ) {
                    targetEle[eventName] = function() {
                        var errorCount = PageValidateUtil.initImidValidates(validateFuncs, args);
                        return errorCount <= 0;
                    }
                }
            }
        }
    },
    //初始化验证：包括表单提交验证，及时验证和获取焦点验证
    //validateMode:0x111,0-7之间的整数，
    // 0x100:表单提交验证
    // 0x010:及时验证
    // 0x001:获取焦点验证
    initValidates: function(validateFuncs, args, validateMode) {
        if ((validateMode & 1) > 0) {
            PageValidateUtil.initFocusValidates(validateFuncs, args);
        }
        if ((validateMode & 2) > 0) {
            PageValidateUtil.initImidValidates(validateFuncs, args);
        }
        if ((validateMode & 4) > 0) {
            PageValidateUtil.initSubmitValidates(validateFuncs, args);
        }
    },
    clearEventBubble: function(evt) {
        evt = evt || window.event;

        if (evt.stopPropagation) {
            evt.stopPropagation();
        } else {
            evt.cancelBubble = true;
        }

        if (evt.preventDefault) {
            evt.preventDefault();
        } else {
            evt.returnValue = false;
        }

    }
}